﻿//This file is part of ILoveHUSTBoard.

//ILoveHUSTBoard is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//ILoveHUSTBoard is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with ILoveHUSTBoard.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebSite.actions
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string username = "";
            string password = "";

            bool isInputValid = true;
            try
            {
                //验证参数合法性
                if (string.IsNullOrEmpty((string)Request["username"]) || string.IsNullOrEmpty((string)Request["password"]))
                    throw new ArgumentOutOfRangeException("request_values", "传入参数非法");

                username = Request["username"];
                password = Request["password"];
            }
            catch
            {
                isInputValid = false;
            }

            if (isInputValid)
            {
                DataHandler.user.UserAccessProcessor userAccessProcessor = new DataHandler.user.UserAccessProcessor();
                if (userAccessProcessor.ValidateUser(username, password))
                {
                    Session["login"] = username;
                    Response.Write("success");
                }
                else
                {
                    Session["login"] = null;
                    Response.Write("fail");
                    Response.Redirect("/index.aspx?error=loginerror");
                }
            }
            else
            {
                Response.Write("error");
                Response.Redirect("/index.aspx?error=unknown");
            }
            if (Request["returnUrl"] != null)
                Response.Redirect(Request["returnUrl"]);
            else
                Response.Redirect("/index.aspx");
        }
    }
}